/*
 * Copyright (c) 2005, The haXe Project Contributors
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

 
/**
 * GD Wrapper allowing for image manipulation
 */
package hxphp.oo.gd;

import php.Lib;

class GD
{	
	
	/** */
	
	public static function gdInfo():Hash<Dynamic>
	{
		return Lib.hashOfAssociativeArray(untyped __call__("gd_info"));
	}
	
	/** function will determine the size of an image */
	public static function getImageSize(filename:String, ?imageinfo):GDImageSizeResult
	{
		var types:Array<GDImageType> = [null, GIF, JPG, PNG, SWF, PSD, BMP, TIFF_II, TIFF_MM, JPC, JP2, JPX, JB2, SWC, IFF, WBMP, XBM];
		var a:Array<Dynamic> = untyped __call__("getimagesize", filename);

		var bits = null;
		var channels = null;
		var mime = null;
		
		try {
			bits = a[4] != null ? a[4] : null;
			channels = a[5] != null ? a[5] : null;
			mime = a[6] != null ? a[6] : null;
		}catch (e:Dynamic) {}
		
		return { width: a[0], height: a[1], type: types[a[2]], bits: bits, channels: channels, mime: mime };
	}
	
	/** Get file extension for image an GDImageType */ 
	public static function imageTypeToExtension ( imageType:GDImageType, ?include_dot:Bool ):String
	{
		var t = switch(imageType)
		{
			case GDImageType.GIF: "gif";
			case GDImageType.JPG: "jpg";
			case GDImageType.PNG: "png";
			case GDImageType.SWF: "swf";
			case GDImageType.PSD: "psd";
			case GDImageType.BMP: "bmp";
			case GDImageType.TIFF_II: "tiff";
			case GDImageType.TIFF_MM: "tiff";
			case GDImageType.JPC: "jpc";
			case GDImageType.JP2: "jp2";
			case GDImageType.JPX: "jpf";
			case GDImageType.JB2: "jb2";
			case GDImageType.SWC: "swc";
			case GDImageType.IFF: "aiff";
			case GDImageType.WBMP: "wbmp";
			case GDImageType.XBM: "xbm";
		}
		return include_dot ? "." + t : t;
	}

	/** determine the Mime-Type for an GDImageType */
	public static function imageTypeToMimeType(imageType:GDImageType):String
	{
		return switch(imageType)
		{
			case GDImageType.GIF: "image/gif";
			case GDImageType.JPG: "image/jpeg";
			case GDImageType.PNG: "image/png";
			case GDImageType.SWF: "application/x-shockwave-flash";
			case GDImageType.PSD: "image/psd";
			case GDImageType.BMP: "image/psd";
			case GDImageType.TIFF_II: "image/tiff";
			case GDImageType.TIFF_MM: "image/tiff";
			case GDImageType.JPC: "image/octet-stream";
			case GDImageType.JP2: "image/jp2";
			case GDImageType.JPX: "image/octet-stream";
			case GDImageType.JB2: "image/octet-stream";
			case GDImageType.SWC: "image/x-shockwave-flash";
			case GDImageType.IFF: "image/iff";
			case GDImageType.WBMP: "image/vnd.wap.wbmp";
			case GDImageType.XBM: "image/xbm";
		}
	}
	
	
	/** Return the image types supported by this PHP build   */
	public static inline function imageTypes():Int
	{
		return untyped __call__("imageTypes");
	}
	
	/** Embed binary IPTC data into a JPEG image */
	public static inline function iptcEmbed ( iptcdata:String, jpeg_file_name:String, ?spool:Int ):Dynamic
	{
		return untyped __call__("iptcembed", iptcdata, jpeg_file_name, spool);
	}
	
	/** Parse a binary IPTC http://www.iptc.org/ block into single tags.  */
	public static inline function iptcParse ( iptcblock:String ):Array<Dynamic>
	{
		return untyped __call__("iptcparse", iptcblock);
	}
	
	/** Convert JPEG image file to WBMP image file  */
	public static inline function jpeg2wbmp ( jpegname:String, wbmpname:String, d_height:Int, d_width:Int, threshold:Int):Int
	{
		return untyped __call__("jpeg2wbmp", jpegname, wbmpname, d_height, d_width, threshold);
	}
	
	/** Convert PNG image file to WBMP image file  */
	public static inline function png2wbmp ( pngname:String, wbmpname:String, d_height:Int, d_width:Int, threshold:Int ):Int
	{
		return untyped __call__("png2wbmp", pngname, wbmpname, d_height, d_width, threshold);
	}
	
}

typedef GDImageSizeResult = {
	var width:Int;
	var height:Int;
	var type:GDImageType;
	var bits:Int;
	var channels:Int;
	var mime:String;
}

enum GDImageType
{
	GIF;
	JPG;
	PNG;
	SWF;
	PSD;
	BMP;
	TIFF_II;
	TIFF_MM;
	JPC;
	JP2;
	JPX;
	JB2;
	SWC;
	IFF;
	WBMP;
	XBM;
}